<template>
  <div class="role-management">
    <el-card>
      <div class="header">
        <h1>角色管理</h1>
        <el-button type="primary" @click="handleAdd">新增角色</el-button>
      </div>
      
      <el-table :data="roleList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="roleName" label="角色名称" />
        <el-table-column prop="permissions" label="权限描述" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="formData" :rules="rules" ref="formRef">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="formData.roleName" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限描述" prop="permissions">
          <el-input 
            v-model="formData.permissions" 
            type="textarea" 
            placeholder="请输入权限描述" 
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  listRole, 
  addRole, 
  updateRole, 
  delRole 
} from '@/api/role'

const roleList = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const formData = ref({
  id: null,
  roleName: '',
  permissions: ''
})

const rules = {
  roleName: [
    { required: true, message: '请输入角色名称', trigger: 'blur' }
  ]
}

// 加载角色列表
const loadRoles = async () => {
  try {
    const response = await listRole()
    if (response) {
      roleList.value = response
    } else {
      ElMessage.error('获取角色列表失败: 数据格式异常')
    }
  } catch (error) {
    ElMessage.error(`获取角色列表失败: ${error.message}`)
  }
}

// 新增角色
const handleAdd = () => {
  dialogTitle.value = '新增角色'
  formData.value = {
    id: null,
    roleName: '',
    permissions: ''
  }
  dialogVisible.value = true
}

// 编辑角色
const handleEdit = (row) => {
  dialogTitle.value = '编辑角色'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除角色
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm(`确定删除角色【${row.roleName}】吗?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await delRole(row.id)
    ElMessage.success('删除成功')
    loadRoles()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 提交表单
const submitForm = async () => {
  try {
    await formRef.value.validate()
    
    if (formData.value.id) {
      await updateRole(formData.value)
      ElMessage.success('更新成功')
    } else {
      await addRole(formData.value)
      ElMessage.success('新增成功')
    }
    
    dialogVisible.value = false
    loadRoles()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
    }
  }
}

onMounted(() => {
  loadRoles()
})
</script>

<style scoped>
.role-management {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>